package com.wxx;

/**
 * @author: Jetwang
 * @date: 2022/3/17 19:09
 * @description: LinearSearch 线性查找
 * @version: 1.0
 */
public class LinearSearch {

    /**
     * 将构造器私有
     */
    private LinearSearch() { }

    /**
     * 线性查找
     * @param data 数组
     * @param target 目标值
     * @return
     */
    public static int search(int[] data, int target) {
        for (int i = 0; i < data.length; i++) {
            if (target == data[i]) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 6, 3};
        int index = LinearSearch.search(arr, 4);
        System.out.println(index);
    }
}
